package com.yang.leather.mall.action;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.yang.leather.admin.pojo.Product;
import com.yang.leather.admin.service.IProdService;
import com.yang.leather.common.page.Pagination;
import com.yang.leather.common.struts2.ContextPvd;
import com.yang.leather.common.util.RequestUtil;

@Scope("prototype")
@Controller("mall.prod")
public class ProdAction extends MallBaseAction{

	private static final long serialVersionUID = 1L;
	
	private IProdService prodService;
	
	private ContextPvd contextPvd;
	
	public String products(){
		Map<String,String> param = RequestUtil.getMapByRequest(this.getRequest());
		
		int pageNo = 1;
		if(StringUtils.isNotEmpty(param.get("curPage"))){
			pageNo = Integer.parseInt(param.get("curPage"));
		}
		int limit = 9;
		if(StringUtils.isNotEmpty(param.get("perPage"))){
			limit = Integer.parseInt(param.get("perPage"));
		}
		
		Pagination page = prodService.getProductListPage(param, pageNo, limit);
		this.getRequest().setAttribute("page", page);
		
		this.loadCommonInfo();
		
		this.getRequest().setAttribute("menuSelect", "products");
		
		return SUCCESS;
	}
	
	public String product(){
		String prodId = this.getRequest().getParameter("pid");
		Product product = this.prodService.findProductById(Long.valueOf(prodId));
		this.getRequest().setAttribute("product", product);
		
		this.loadCommonInfo();
		
		return SUCCESS;
	}

	public IProdService getProdService() {
		return prodService;
	}

	@Autowired
	public void setProdService(@Qualifier("prodService")
			IProdService prodService) {
		this.prodService = prodService;
	}
	
	public ContextPvd getContextPvd() {
		return contextPvd;
	}

	@Autowired
	public void setContextPvd(@Qualifier("contextPvd")
			ContextPvd contextPvd) {
		this.contextPvd = contextPvd;
	}
}
